﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using GWEB.WebBase.Data;
using GWEB.WebBase.Entity;
using GWEB.WebBase.Utility;
using GWEB.WebFrame.Public;

namespace GWEB.WebFrame.Customer
{
    public class Import1Controller : Controller
    {
       // [ActionFilter(IsLogin = true)]
        public ActionResult Index(String custFromId, String custFromRemark, String file)
        {
            ResultData result=ResultData.Error("操作异常");          
            file = Fun.cString(file);
            if (!file.Equals(""))
            {
                String fieldExcel = String.Format(@"宝宝姓名 as CustName,
宝宝小名 as ChildName,
出生日期 as BirdDate,
性别 as Sex,
妈妈 as LinkName,
妈妈手机号码 as CustTel,
爸爸 as LinkName1,
爸爸手机号码 as CustTel1,
微信号 as WeiXin,
固定电话 as Gddh,
地址 as Address,
市场人员 as MarketName,
意向课程 as IntentionCourse,
意向时间 as IntentionDate,
重要程度 as Importance,
'{0}' as CustFromId,
'{1}' as CustFromRemark", custFromId,custFromRemark);
                String fileName = file.Split('|')[1];
                String fullFileName = GWEB.WebBase.Utility.File.GetPath() + fileName.Substring(3);
                result = ExcelToData(BasePage.GetUserId(Session), "cu_Customers_Temp", fullFileName, fieldExcel);
                if (result.Check())
                {
                    result = ProdData(0, BasePage.GetUserId(Session), "Import_Customer");
                }
            }
            else {
                result = ResultData.Error("请选择上传文件");               
            }           
            ViewBag.ResultData = result;
            ViewBag.PageSize = Config.PageSize;
            return View(BasePage.V("Customer/Import1"));
        }
        //导入数据
        [ActionFilter(IsLogin = true)]
        public JsonResult Finish()
        {
            ResultData result = ProdData(1, BasePage.GetUserId(Session), "Import_Customer");
            result.data = null;//不传数据
            return BasePage.Json(result);
        }
        //将Excel导入到时表
         ResultData ExcelToData(String iUserId, String TableNameTemp, String FullFileName, String FieldExcel)
        {          
            DataSet ds = Excel.ExecuteQuery(FullFileName, "select " + iUserId + " as iUserId," + FieldExcel + " from ExcelTable t");
            if (ds == null) { return ResultData.Error(Excel.Error.Replace("<IMG alt='' src='../images/error.gif' align='absmiddle'> ", "")); }
            ds.Tables[0].TableName = TableNameTemp;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                dr.SetAdded();
            }

            BaseData bd = new BaseData();
            bd.ExecuteSQL("delete " + TableNameTemp + " where iuserid=" + iUserId);
            if (bd.UpdateDataSet(ds) == false) { return ResultData.Error("数据库连接失败或导入数据长度、类型出错、没有任何记录"); }
            return ResultData.OK();
        }
        //检查或导入数据
        ResultData ProdData(int type, string iuserid, string procName)
        {  
            SqlParameter[] para = new SqlParameter[3];

            para[0] = new SqlParameter();
            para[0].ParameterName = "type";
            para[0].SqlDbType = SqlDbType.Int;
            para[0].SqlValue = type;

            para[1] = new SqlParameter();
            para[1].ParameterName = "iuserid";
            para[1].SqlDbType = SqlDbType.VarChar;
            para[1].Size = 20;
            para[1].Value = iuserid;

            para[para.Length - 1] = new SqlParameter();
            para[para.Length - 1].Direction = ParameterDirection.ReturnValue;

            DataSet ds = BasePage.bd.ProcDataSet(procName, para);
            
            if (ds == null)
            {
                return ResultData.Error("字段名称、长度不符");
            }
            //List<Dictionary<String, Object>> data = BasePage.DataTableToList(ds.Tables[0]);

            if (para[para.Length - 1].Value.ToString() == "0")
            {
                ResultData result = ResultData.Error("数据有错误");                
                return result;
            }
            if (para[para.Length - 1].Value.ToString() == "1")
            {
                //检查成功直接跳转
                DataTable dt = ds.Tables[0];
                int count = dt.Rows.Count;
                int overCount = dt.Select("error<>''").Length;
                Dictionary<String, Object> data = new Dictionary<String, Object>();
                data.Add("count", count);
                data.Add("overCount", overCount);
                data.Add("data", dt);
                return ResultData.OK(data);
            }
            if (para[para.Length - 1].Value.ToString() == "2")
            {
                //导入成功直接跳转
                return ResultData.OK();
            }
            return ResultData.Error("操作异常");
        }
    }
}